package myLinkedList;
/*
* 双向单链表的实现
* */
public class MyLinkList {

    static class ListNode{
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head;
    ListNode last;
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
        } else {
            node.prev = last;
            last.next = node;
            last = node;
        }
    }
    //任意位置插入，第一个节点为0号下标
    public void addIndxe(int index,int data){
        ListNode cur = head;
        ListNode node = new ListNode(data);
        if(index < 0 || index > size()){
            System.out.println("插入位置不合法");
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        while(index-- > 0){
            cur = cur.next;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }
    //查找是否包含关键字key在链表中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //是头结点
                if(cur == head){
                    head = head.next;
                    //考虑只有一个头结点的情况
                    if(head != null){
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else{
                    if(cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                        //是尾结点
                    } else {
                        last = last.prev;
                        last.next = null;
                    }
                }
                return; //删除完之后循环结束
            } else {
                cur = cur.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAll(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //是头结点
                if(cur == head){
                    head = head.next;
                    //考虑只有一个头结点的情况
                    if(head != null){
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else{
                    if(cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                        //是尾结点
                    } else {
                        last = last.prev;
                        last.next = null;
                    }
                }
            }
            cur = cur.next; //删除完之后继续向后走，遍历完整个链表
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }

        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode curNext  = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}
